JHipster中使用Config Server

JHipster中当启动一个微服务或者网关时,首先会连接到JHipster Registry去获取相应的配置信息,而微服务工程下的配置信息将被覆盖(只覆盖某个配置项的值,不会覆盖整个文件),可在http://localhost:8761/#/config下查看各个微服务在JHipster Registry中的配置信息

  • 开发环境一般使用本地文件系统来保存
  • 生产环境使用git仓库服务器上的配置

保存配置文件的地址需要在jhipster-registry下的application-dev.yml中配置,配置如下:

spring:
    profiles:
        active: dev
        include: native
    cloud:
        config:
            server:
                native:
                     # 绝对路径配置:search-locations: file:///E:/config-repo
                     search-locations: file:./central-config
                git:
                    uri: https://github.com/chuiliu/config-repo

以上配置项说明:

  • spring.profiles.active:dev:使用开发环境的配置
  • spring.profiles.include:git:使用本地文件系统的配置,如果要使用远程git仓库的配置,则需修改spring.profiles.include值为git

默认命名规则:{微服务名}[-dev|prod].yml
此外,命名为application[-dev|prod].yml的配置文件将对所有注册到注册中心的微服务和网关起作用,可以在其中配置各个微服务所共有的信息。

例如:

在名为gateway的网关的生产环境的配置文件应命名为:gateway-prod.yml
名为users的微服务的配置文件则命名为:users-prod.yml

可在各个微服务项目下的bootstrap[-dev|prod].yml文件中修改以下配置项来定义配置文件名:

spring:
    cloud:
        config:
            name: newFilename

测试users微服务:

package com.mycompany.myapp.web.rest;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Administrator on 2016/8/30.
 */
@RestController
@RequestMapping("/api")
@Api(value = "/api", description = "test", position = 1)
// 测试自动刷新配置
@ConfigurationProperties
public class testConfigResource {

    @Value("${testData.user.id:defaultValue}")
    private String id;

    @Value("${testData.user.name:defaultValue}")
    private String name;

    @Value("${testData.user.remark:defaultValue}")
    private String remark;

    @RequestMapping(value = "test_id", method = RequestMethod.GET)
    public String testIntroduction() {
        return "id:" + id;
    }

    @RequestMapping(value = "test_name", method = RequestMethod.GET)
    public String testName() {
        return "name:" + name;
    }

    @RequestMapping(value = "test_remark", method = RequestMethod.GET)
    public String testRemark() {
        return "remark:" + remark;
    }
}

项目配置如下:

  • users微服务工程下application-dev.yml的配置项:

    testData:
      user:
          id: users微服务项目下配置文件中的id值
          name: users微服务项目下配置文件中的name值
          remark: users微服务项目下配置文件中的remark值(在本地文件系统中的配置和git远程仓库中均没有配置这个值)
    
  • jhipster-registry下的application-dev.yml中配置:

    spring:
      profiles:
          active: dev
          include: git
      cloud:
          config:
              server:
                  git:
                      uri: https://github.com/chuiliu/config-repo
    
  • 本地文件系统file:./central-configusers.yml的配置项:

    testData:
      user:
          id: 本地文件系统配置文件中的id值
          name: 本地文件系统配置文件中的name值
    
  • 远程git仓库下users-dev.yml的配置项:

    testData:
      user:
          id: git远程仓库下配置文件中的id值
          name: git远程仓库下配置文件中的name值
    

在swagger测试结果如下:
http://o743aqnrb.bkt.clouddn.com/config%20server%20testtest-id.png

http://o743aqnrb.bkt.clouddn.com/config%20server%20testtest-name.png

因为远程git仓库下没有配置remark的值,因此取到的值是微服务工程下配置的:
http://o743aqnrb.bkt.clouddn.com/config%20server%20testtest-reamrk.png

参考:http://www.infoq.com/cn/articles/spring-cloud-service-wiring